home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir42 / gnudbm14.zip / FINDKEY.C < prev    next >
C/C++ Source or Header  |  1990-08-24  |  7KB  |  188 lines

  1. /* findkey.c - The routine that finds a key entry in the file. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.        
  28. *************************************************************************/
  29.  
  30. /*
  31.  * MS-DOS port (c) 1990 by Thorsten Ohl, td12@@ddagsi3.bitnet
  32.  *
  33.  * To this port, the same copying conditions apply as to the
  34.  * original release.
  35.  *
  36.  * IMPORTANT:
  37.  * This file is not identical to the original GNU release!
  38.  * You should have received this code as patch to the official
  39.  * GNU release.
  40.  *
  41.  * MORE IMPORTANT:
  42.  * This port comes with ABSOLUTELY NO WARRANTY.
  43.  *
  44.  * $Header: e:/gnu/gdbm/RCS/findkey.c'v 1.4.0.1 90/08/16 09:22:23 tho Exp $
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <sys/types.h>
  49. #ifndef MSDOS
  50. #include <sys/file.h>
  51. #endif /* not MSDOS */
  52. #include <sys/stat.h>
  53. #include "gdbmdefs.h"
  54. #include "systems.h"
  55.  
  56.  
  57. /* Read the data found in bucket entry ELEM_LOC in file DBF and
  58.    return a pointer to it.  Also, cache the read value. */
  59.  
  60. char *
  61. _gdbm_read_entry (dbf, elem_loc)
  62.      gdbm_file_info *dbf;
  63.      int elem_loc;
  64. {
  65.   LONG num_bytes;        /* For seeking and reading. */
  66.   int key_size;
  67.   int data_size;
  68.   data_cache_elem *data_ca;
  69.  
  70.   /* Is it already in the cache? */
  71.   if (dbf->cache_entry->ca_data.elem_loc == elem_loc)
  72.     return dbf->cache_entry->ca_data.dptr;
  73.  
  74.   /* Set sizes and pointers. */
  75.   key_size = dbf->bucket->h_table[elem_loc].key_size;
  76.   data_size = dbf->bucket->h_table[elem_loc].data_size;
  77.   data_ca = &dbf->cache_entry->ca_data;
  78.   
  79.   /* Set up the cache. */
  80.   if (data_ca->dptr != NULL) free (data_ca->dptr);
  81.   data_ca->key_size = key_size;
  82.   data_ca->data_size = data_size;
  83.   data_ca->elem_loc = elem_loc;
  84.   data_ca->hash_val = dbf->bucket->h_table[elem_loc].hash_value;
  85.   data_ca->dptr = (char *) malloc (key_size+data_size);
  86.   if (data_ca->dptr == NULL) _gdbm_fatal (dbf, "malloc error");
  87.  
  88.  
  89.   /* Read into the cache. */
  90.   num_bytes = lseek (dbf->desc,
  91.              dbf->bucket->h_table[elem_loc].data_pointer, L_SET);
  92.   if (num_bytes != dbf->bucket->h_table[elem_loc].data_pointer)
  93.     _gdbm_fatal (dbf, "lseek error");
  94.   num_bytes = read (dbf->desc, data_ca->dptr, key_size+data_size);
  95.  
  96.   if (num_bytes != key_size+data_size) _gdbm_fatal (dbf, "read error");
  97.  
  98.   return data_ca->dptr;
  99. }
  100.  
  101.  
  102.  
  103. /* Find the KEY in the file and get ready to read the associated data.  The
  104.    return value is the location in the current hash bucket of the KEY's
  105.    entry.  If it is found, a pointer to the data and the key are returned
  106.    in DPTR.  If it is not found, the value -1 is returned.  Since find
  107.    key computes the hash value of key, that value */
  108. int
  109. _gdbm_findkey (dbf, key, dptr, new_hash_val)
  110.      gdbm_file_info *dbf;
  111.      datum key;
  112.      char **dptr;
  113.      LONG   *new_hash_val;        /* The new hash value. */
  114. {
  115.   LONG   bucket_hash_val;    /* The hash value from the bucket. */
  116.   char  *file_key;        /* The complete key as stored in the file. */
  117.   int    elem_loc;        /* The location in the bucket. */
  118.   int    home_loc;        /* The home location in the bucket. */
  119.   int    key_size;        /* Size of the key on the file.  */
  120.  
  121.   /* Compute hash value and load proper bucket.  */
  122.   *new_hash_val = _gdbm_hash (key);
  123. #ifdef MSDOS            /* careful!!!! */
  124.   _gdbm_get_bucket (dbf, (int) (*new_hash_val>> (31-dbf->header->dir_bits)));
  125. #else /* not MSDOS */
  126.   _gdbm_get_bucket (dbf, *new_hash_val>> (31-dbf->header->dir_bits));
  127. #endif /* not MSDOS */
  128.  
  129.   /* Is the element the last one found for this bucket? */
  130.   if (*new_hash_val == dbf->cache_entry->ca_data.hash_val
  131.       && dbf->cache_entry->ca_data.key_size == key.dsize
  132.       && dbf->cache_entry->ca_data.dptr != NULL
  133.       && bcmp (dbf->cache_entry->ca_data.dptr, key.dptr, key.dsize) == 0)
  134.     {
  135.       /* This is it. Return the cache pointer. */
  136.       *dptr = dbf->cache_entry->ca_data.dptr+key.dsize;
  137.       return dbf->cache_entry->ca_data.elem_loc;
  138.     }
  139.       
  140.   /* It is not the cached value, search for element in the bucket. */
  141. #ifdef MSDOS            /* careful!!!  */
  142.   elem_loc = (int) (*new_hash_val % dbf->header->bucket_elems);
  143. #else /* not MSDOS */
  144.   elem_loc = *new_hash_val % dbf->header->bucket_elems;
  145. #endif /* not MSDOS */
  146.   home_loc = elem_loc;
  147.   bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
  148.   while (bucket_hash_val != -1)
  149.     {
  150.       key_size = dbf->bucket->h_table[elem_loc].key_size;
  151.       if (bucket_hash_val != *new_hash_val
  152.      || key_size != key.dsize
  153.      || bcmp (dbf->bucket->h_table[elem_loc].key_start, key.dptr,
  154.             (SMALL < key_size ? SMALL : key_size)) != 0) 
  155.     {
  156.       /* Current elem_loc is not the item, go to next item. */
  157.       elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
  158.       if (elem_loc == home_loc) return -1;
  159.       bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
  160.     }
  161.       else
  162.     {
  163.       /* This may be the one we want.
  164.          The only way to tell is to read it. */
  165.       file_key = _gdbm_read_entry (dbf, elem_loc);
  166.       if (bcmp (file_key, key.dptr, key_size) == 0)
  167.         {
  168.           /* This is the item. */
  169.           *dptr = file_key+key.dsize;
  170.           dbf->cache_entry->ca_data.hash_val = *new_hash_val;
  171.           return elem_loc;
  172.         }
  173.       else
  174.         {
  175.           /* Not the item, try the next one.  Return if not found. */
  176.           elem_loc = (elem_loc + 1) % dbf->header->bucket_elems;
  177.           if (elem_loc == home_loc) return -1;
  178.           bucket_hash_val = dbf->bucket->h_table[elem_loc].hash_value;
  179.         }
  180.     }
  181.     }
  182.  
  183.   /* If we get here, we never found the key. */
  184.   return -1;
  185.  
  186. }
  187.  
  188.